Skip to content

Method: checkPossibleOutcome(Map, boolean, boolean)

1: package de.fhdw.gaming.ipspiel22.kopfundzahl.domain.impl;
2:
3: import java.util.Collections;
4: import java.util.Map;
5: import java.util.Optional;
6:
7: import de.fhdw.gaming.core.domain.GameException;
8: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlPlayer;
9: import de.fhdw.gaming.ipspiel22.kopfundzahl.domain.KopfundZahlPlayerBuilder;
10:
11: /**
12: * Implements {@link KopfundZahlPlayerBuilder}.
13: */
14: public final class KopfundZahlPlayerBuilderImpl implements KopfundZahlPlayerBuilder {
15:
16: /**
17: * The name of the player.
18: */
19: private Optional<String> name;
20:
21: /**
22: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
23: * for the second-level map is the answer of the second player.
24: */
25: private Optional<Map<Boolean, Map<Boolean, Double>>> possibleOutcomes;
26:
27: /**
28: * Creates an {@link KopfundZahlPlayerBuilderImpl}.
29: */
30: public KopfundZahlPlayerBuilderImpl() {
31: this.name = Optional.empty();
32: this.possibleOutcomes = Optional.empty();
33: }
34:
35: @Override
36: public KopfundZahlPlayerBuilderImpl changeName(final String newName) {
37: this.name = Optional.of(newName);
38: return this;
39: }
40:
41: @Override
42: public KopfundZahlPlayerBuilder changePossibleOutcomes(final Map<Boolean, Map<Boolean,
43: Double>> newpossibleOutcomes) {
44: this.possibleOutcomes = Optional.of(newpossibleOutcomes);
45: return this;
46: }
47:
48: @Override
49: public KopfundZahlPlayer build() throws GameException {
50: return new KopfundZahlPlayerImpl(
51: this.name.orElseThrow(),
52: this.checkPossibleOutcomes(this.possibleOutcomes.orElseThrow()));
53: }
54:
55: /**
56: * Checks if all possible outcomes are defined for a player.
57: *
58: * @param outcomes The possible outcomes for the player.
59: */
60: private Map<Boolean, Map<Boolean, Double>> checkPossibleOutcomes(
61: final Map<Boolean, Map<Boolean, Double>> outcomes) {
62: this.checkPossibleOutcome(outcomes, false, false);
63: this.checkPossibleOutcome(outcomes, false, true);
64: this.checkPossibleOutcome(outcomes, true, false);
65: this.checkPossibleOutcome(outcomes, true, true);
66: return outcomes;
67: }
68:
69: /**
70: * Checks if a given outcome is defined for a player.
71: *
72: * @param outcomes The possible outcomes for the player.
73: * @param firstChoice The choice of the first player.
74: * @param secondChoice The choice of the second player.
75: */
76: private void checkPossibleOutcome(final Map<Boolean, Map<Boolean, Double>> outcomes, final boolean firstChoice,
77: final boolean secondChoice) {
78:• if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
79: throw new IllegalArgumentException(
80: String.format(
81: "No outcome defined for player '%s' and combination %s/%s.",
82: this.name,
83: toAnswer(firstChoice),
84: toAnswer(secondChoice)));
85: }
86: }
87:
88: /**
89: * Maps a boolean value to a "head" or "tail" answer.
90: *
91: * @param value The value to be mapped.
92: */
93: private static String toAnswer(final boolean value) {
94: return value ? "head" : "tail";
95: }
96:
97: }